package com.yijiu.trb;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.BaseResponse;

/**
 * 激活页面
 * 
 * @author TangChao_Lenovo
 */
public class ActiveActivity extends BTitleActivity
{
	private EditText pwdEditText, pwdAgainEditText, payPwdEditText, payPwdAgainEditText, codeEditText, phoneEditText;

	private String codeStr;

	/**
	 * 电话
	 */
	private String phoneStr;

	private String pwdStr, payPwdStr;

	private Button sendCode;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_active);
		initWidget();
	}

	@Override
	protected void initWidget()
	{
		titleView.setText("激活账户");
		TextView realName = (TextView) findViewById(R.id.tv_regist_two_name);
		pwdEditText = (EditText) findViewById(R.id.et_regist_two_pwd);
		pwdAgainEditText = (EditText) findViewById(R.id.et_regist_two_pwd_again);
		payPwdEditText = (EditText) findViewById(R.id.et_regist_two_pay_pwd);
		payPwdAgainEditText = (EditText) findViewById(R.id.et_regist_two_pay_pwd_again);
		codeEditText = (EditText) findViewById(R.id.et_regist_two_code);
		phoneEditText = (EditText) findViewById(R.id.et_regist_two_phone);
		sendCode = (Button) findViewById(R.id.btn_regist_two_send_again);
		Intent intent = getIntent();
		realName.setText(intent.getStringExtra("realNmaeStr"));
	}

	private void doRegistAction()
	{
		pwdStr = pwdEditText.getText().toString();
		String pwdAgainStr = pwdAgainEditText.getText().toString();
		payPwdStr = payPwdEditText.getText().toString();
		String payPwdAgainStr = payPwdAgainEditText.getText().toString();
		codeStr = codeEditText.getText().toString();
		phoneStr = phoneEditText.getText().toString();
		if (StringUtils.isNull(pwdStr))
		{
			app.showMsg("请输入登录密码");
			return;
		}
		if (!StringUtils.checkHasLetNum(pwdAgainStr) || !StringUtils.checkHasLetNum(pwdStr)
				|| (pwdAgainStr.length() < 6 || pwdAgainStr.length() > 16) || (pwdStr.length() < 6 || pwdStr.length() > 16))
		{
			app.showMsg("密码应该为6~16位字符，至少包含数字、字母（区分大小写）");
			return;
		}
		if (StringUtils.isNull(pwdAgainStr))
		{
			app.showMsg("请再次输入登录密码");
			return;
		}
		if (!pwdStr.equals(pwdAgainStr))
		{
			app.showMsg("两次输入的登录密码不一样");
			return;
		}
		if (StringUtils.isNull(payPwdStr))
		{
			app.showMsg("请输入支付密码");
			return;
		}
		if (!StringUtils.checkHasLetNum(payPwdStr) || !StringUtils.checkHasLetNum(payPwdAgainStr))
		{
			app.showMsg("支付密码应该为字母和数字的组合");
			return;
		}
		if ((payPwdStr.length() < 6 || payPwdStr.length() > 16) || (payPwdAgainStr.length() < 6 || payPwdAgainStr.length() > 16))
		{
			app.showMsg("支付密码长度应该为6~16位");
			return;
		}
		if (StringUtils.isNull(payPwdAgainStr))
		{
			app.showMsg("请再次输入支付密码");
			return;
		}
		if (!payPwdStr.equals(payPwdAgainStr))
		{
			app.showMsg("两次输入的登录密码不一样");
			return;
		}
		if (StringUtils.isNull(phoneStr))
		{
			app.showMsg("请输入手机号码");
			return;
		}
		if (!StringUtils.isPhoneNumberValid(phoneStr))
		{
			app.showMsg("请输入有效的手机号码");
			return;
		}
		if (StringUtils.isNull(codeStr))
		{
			app.showMsg("请输入验证码");
			return;
		}
		dataCheck();
	}

	public void registTwoClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btn_regist_two_send_again:// 发送验证码
				doSendAgainDataCheck();
				break;
			case R.id.btn_regist_two_active:// 激活
				doRegistAction();
				break;
			default:
				break;
		}
	}

	/**
	 * 发送验证码
	 * 
	 * @param phoneStr
	 */
	private void doSendAgainDataCheck()
	{
		phoneStr = phoneEditText.getText().toString();
		if (StringUtils.isNull(phoneStr))
		{
			app.showMsg("请输入手机号码");
			return;
		}
		if (!StringUtils.isPhoneNumberValid(phoneStr))
		{
			app.showMsg("请输入有效的手机号码");
			return;
		}
		String inputStr = "service=appMobileService&mobile=" + phoneStr + "&business=ACTIVATE";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction baseAction = new BaseAction(encodedData, encriptData);
		BaseResponse baseResponse = new BaseResponse();
		Request r = new Request(baseAction, baseResponse, ActionType.SEND_AGAIN);
		netManager.excute(r, this, this);
		showProgress("正在发送验证码，请稍候！");
	}

	/**
	 * 激活请求
	 */
	private void dataCheck()
	{
		String inputStr = "service=appUserActive&MD5UserBaseId=" + getIntent().getStringExtra("md5Id") + "&verifyCode=" + codeStr
				+ "&logPassword=" + pwdStr + "&payPassword=" + payPwdStr + "&mobile=" + phoneStr;
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData);
		BaseResponse baseResponse = new BaseResponse();
		// ActiveResponse activeResponse = new ActiveResponse();
		Request r = new Request(action, baseResponse, ActionType.ACTIVE_CODE);
		netManager.excute(r, this, this);
		showProgress("正在激活，请稍候！");
	}

	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.ACTIVE_CODE:
				app.showMsg("激活成功！");
				BaseResponse baseResponse = (BaseResponse) r.getResponse();
				CacheData.APP_KEY = baseResponse.field.appKey;
				initIntent(this, ActiveDoneActivity.class);
				finish();
				break;
			case ActionType.SEND_AGAIN:
				app.showMsg("发送验证码成功！");
				timerCount(sendCode);
				break;
		}
	}

	/**
	 * 操作失败返回的函数
	 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.REGIST_CODE:
			case ActionType.SEND_AGAIN:
				app.showMsg(errorMsg);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
}
